<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery.state" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1">待处理({{waitCount}})</el-menu-item>
      <el-menu-item index="2">已处理({{ finishCount  }})</el-menu-item>
      <el-menu-item index="3">我发起的({{ myCheckCount }})</el-menu-item>
      <el-menu-item index="4">我收到的({{ myApplyCount }})</el-menu-item>
      <el-menu-item index="" v-if="isAllSale">全部({{ allCount }})</el-menu-item>
    </el-menu>
    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="80px">
        <el-form-item label="关键字：">
          <el-input v-model="listQuery.keyword" placeholder="内容关键字:客户名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="审批类型：">
          <el-select v-model="listQuery.checkType" style="width: 180px;" placeholder="选择审批类型" clearable>
            <el-option value="payTypeApply" label="结算方式申请"></el-option>
<!--            <el-option value="orderCancelApply" label="订单取消申请"></el-option>-->
            <el-option value="WXBankAccountApply" label="锡商账期申请"></el-option>
            <el-option value="delaySendApply" label="预售订单关闭延期申请"></el-option>
            <el-option value="delaySettleOrderApply" label="后结算下单申请"></el-option>
            <el-option value="delaySettleApply" label="订单后结算申请"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="申请人：" v-if="listQuery.state !='3'" clearable>
          <el-input v-model="listQuery.userName" placeholder="请输入申请人" clearable></el-input>
        </el-form-item>
        <el-form-item label="审批状态：" v-if="listQuery.state != '1'">
          <el-select v-model="listQuery.checkState" style="width: 180px;" placeholder="选择审批状态" clearable>
            <el-option value="0" label="待部门负责人确认"></el-option>
            <el-option value="8" label="销售员确认"></el-option>
            <el-option value="10" label="待采购产品线负责人确认"></el-option>
            <el-option value="20" label="待采购部门负责人确认"></el-option>
            <el-option value="30" label="待财务负责人确认"></el-option>
            <el-option value="40" label="待总经理确认"></el-option>
            <el-option value="50" label="待申请人上传补充协议"></el-option>
            <el-option value="55" label="待部门负责人确认补充协议"></el-option>
            <el-option value="100" label="审批通过"></el-option>
            <el-option value="-1" label="审批不通过"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleSearchList()" type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-container">
      <el-table v-loading="listLoading" :data="tableData" border style="width: 100%; margin-top: 20px">
        <el-table-column prop="checkSn" label="审批编号" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="checkTypeStr" label="审批类型" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="applyUserName" label="申请人" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间" min-width="100" align="center">
        </el-table-column>
        <el-table-column label="审批内容" min-width="180" align="left">
          <template v-slot="{row}">
            <div style="margin:5px 0;"><span style="margin-right: 5px;">客户编号:</span>{{ row.customerNumber }}</div>
            <div style="margin:5px 0;"><span style="margin-right: 5px;">客户名称:</span>{{ row.customerName }}</div>
            <div><span style="margin-right: 5px;">审批内容:</span>{{ row.checkContent }}</div>
          </template>
        </el-table-column>
        <el-table-column label="订单编号" min-width="150" align="center">
          <template v-slot="{row}">
            <el-link @click="toDetail(row)" type="primary">{{ row.orderSn }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="checkState" label="审批状态" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="finishedTime" label="完成时间" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" min-width="100" align="center">
        </el-table-column>
        <el-table-column prop="checkUserName" label="当前审批人" min-width="100" align="center">
        </el-table-column>
        <el-table-column label="操作" min-width="100" align="center">
          <template  slot-scope="{row}">
            <el-button type="text" @click="handleApplyChecks(row)">查看审批</el-button>
            <el-button type="text" v-if="row.isCanUpdate==1" @click="cancleApply(row)">取消申请</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20, 50]"
        :total="count">
      </el-pagination>
    </div>
    <!-- 查看审批 -->
    <handleApplyCheck ref="handleApplyCheck" @upload="getList"></handleApplyCheck>

  </div>
</template>
<script>
import { getCheckListAPI,orderCheckAPI } from '@/api/purchase.js';
import handleApplyCheck from './components/handleApplyCheck.vue';

import { formatDate } from '@/utils/date';
import store from '@/store';
import { mapGetters } from 'vuex';
import axios from "axios";
const defaultListQuery = {
  state: '1',//审核状态
  keyword: '',//关键词
  checkType: '',//类型
  userName: '',//申请人
  checkState:'',
  pageNum: 1,
  pageSize: 10,
};
const defaultForm = {
  checkId:"",
  action: "",
  checkRemark:"",
  sideAgreementUrl:''
};
export default {
  name: "orderPayApplyCheck",
  data() {
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles: store.getters.roles ? store.getters.roles : [],
      allCount: '',//全部
      waitCount: '',//待处理
      finishCount: '',//已处理
      myApplyCount: '',//我申请的
      myCheckCount:'',//我处理的



    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    isAllSale() {
      if (this.roles.indexOf("allPrderPayApplyCheck") != -1) {
        return true
      } else {
        return false
      }
    },
  },
  components: {
    handleApplyCheck,
  },
  created() {
    this.getList();
  },
  filters: {

    // 申请类型
    fifCheckType(val) {
      if (val == 'payTypeCheck') {
        return '结算方式申请'
      } else if (val == 'orderCancel') {
        return '订单取消申请'
      } else if (val == 'WXBankAccountApply') {
        return '锡商账期申请'
      }else if (val == 'delaySendApply') {
        return '预售订单关闭延期申请'
      }
    },
    //审批状态
    fifCheckState(val) {
      if (val == -1) {
        return '审批不通过'
      } else if (val == 0) {
        return '待部门负责人确认'
      } else if (val == 10) {
        return '待采购产品线负责人确认'
      } else if (val == 20) {
        return '待采购部门负责人确认'
      } else if (val == 30) {
        return '待财务负责人确认'
      } else if (val == 40) {
        return '待总经理确认'
      } else if (val == 100) {
        return '审批通过'
      }else if (val == 50) {
        return '待申请人上传补充协议'
      }else if (val == 55) {
        return '待部门负责人确认补充协议'
      }
    }
  },
  methods: {
    toDetail(item){
      // localStorage.setItem('saleListQuery', JSON.stringify(this.listQuery));
      let routeData = this.$router.resolve({name:'saleDetail',query:{id:item.orderId,type:'see'}});
      window.open(routeData.href);
    },
    seeXY(item){
      window.open(item.sideAgreementUrl, '_blank');
    },
    //取消申请
    cancleApply(item) {
      this.$confirm('确定取消此次申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        orderCheckAPI({
          checkId: item.checkId,
          action:"revokeCheck",
          checkRemark: "",
        }).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '取消成功!'
          });
        });
      }).catch(() => {
      });
    },
    // 审批
    handleApplyChecks(item) {
      this.$refs.handleApplyCheck.showPop(item)
    },
    //切换选择状态
    handleSelect(key, keyPath) {
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery.state = key;
      this.handleSearchList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取合同明细列表
    getList() {
      this.listLoading = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery));
      getCheckListAPI(listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.allCount = response.allCount;
        this.finishCount = response.finishCount;
        this.myApplyCount = response.myApplyCount;
        this.myCheckCount = response.myCheckCount;
        this.waitCount = response.waitCount;
        this.listLoading = false;
      }).catch(() => {
        this.listLoading = false;
      });
    },
  },
}
</script>

<style scoped lang="scss">
</style>
